﻿using Books.Common;
using Books.SQLite;
using GalaSoft.MvvmLight;
using Services.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Popups;

namespace Books.ViewModel
{
    public class BookDetailViewModel : ViewModelBase, IBookDetailViewModel
    {
        private IHandleDownLoadFile donwloadFile;
        public IHandleDownLoadFile DonwloadFile
        {
            get
            {
                return donwloadFile;
            }
            set
            {
                donwloadFile = value;
                RaisePropertyChanged("DonwloadFile");
            }
        }
        private bool bookIsDownload;

        public bool BookIsDownload
        {
            get
            {
                return bookIsDownload;
            }
            set
            {
                bookIsDownload = value;
                RaisePropertyChanged("BookIsDownload");
            }
        }
        private string bookId;
        private string deviceId;
        private GetBookDetail dataBooksDetail;
        public GetBookDetail DataBooksDetail
        {
            get
            {
                return dataBooksDetail;
            }
            set
            {
                dataBooksDetail = value;
                RaisePropertyChanged("DataBooksDetail");
            }
        }
        private ObservableCollection<BookList> listDataRecommendBooks;
        public ObservableCollection<BookList> ListDataRecommendBooks
        {
            get
            {
                return listDataRecommendBooks;
            }
            set
            {
                listDataRecommendBooks = value;
                RaisePropertyChanged("ListDataRecommendBooks");
            }
        }
        public BookDetailViewModel(IHandleDownLoadFile donwloadFiles)
        {
            this.donwloadFile = donwloadFiles;
        }
        public async Task<GetBookDetail> GetBookDetails(string bookId, string deviceId)
        {
            var getBookDetail = await App.CallService.GetBookDetailAsync(bookId, deviceId);
            if (getBookDetail != null && getBookDetail.book_detail != null)
            {
                if (getBookDetail.chapter_list.Count > 0)
                {
                    var listData = ManagerFilesDownloadComplete.GetDataDownLoadFiles();
                    foreach (var m in getBookDetail.chapter_list)
                    {
                        if (listData != null)
                        {
                            var checkFilesExits = listData.FirstOrDefault(k => k.FileName == m.chapter_no && k.BookId == bookId);
                            if (checkFilesExits != null)
                                m.isDownload = true;
                        }
                        else
                            m.isDownload = false;
                    }
                }
                return getBookDetail;
            }

            return null;
        }
        public async void SetDataBookDetail(string bookId, string deviceId)
        {
            DataBooksDetail = null;
            this.bookId = bookId;
            this.deviceId = deviceId;

        }
        public async Task<ObservableCollection<BookList>> GetRecommendBooks()
        {
            var getData = await App.CallService.GetRecommendBooks(bookId, "1000");
            if (getData != null && getData.book_list.Count > 0)
                ListDataRecommendBooks = new ObservableCollection<BookList>(getData.book_list);
            return ListDataRecommendBooks;
        }
        public async Task<GetBookDetail> GetData()
        {
            var getData = await GetBookDetails(bookId, deviceId);
            if (getData == null)
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                 () =>
                 {
                     MessageDialog mesage = new MessageDialog("Book ID is invalid");
                     mesage.ShowAsync();
                     return;
                 });

            }
            return getData;
            //GetRecommendBooks(bookId);
            //DataBooksDetail = getData;
        }
      
    }
}
